$(function(){
    //获取宝宝信息
    getChild();
    //切换宝宝
   	$('.baobao').on('change',function(){
		var childIds = $(this).val();
		ones(childIds);
	})
})
//获取宝宝信息
function getChild(){
    ym.ajax({
        url: "/userChild/getUserChild",
        success: function (data) {
            if(data.res == 1){
                $('.baobao').html("");
                if(data.obj.length > 0){
                    var html ="";
                    for(var i=0;i<data.obj.length;i++){
                        if(i == 0){
                            html += '<option value="'+data.obj[i].childId+'" selected>'+data.obj[i].childRealname+'</option>';
                        }else{
                            html += '<option value="'+data.obj[i].childId+'">'+data.obj[i].childRealname+'</option>';
                        }
                    }
                    $('.baobao').append(html);
                    var childId = $(".baobao option:selected").val();
                    console.log(childId);
                    ones(childId);
                }else{
                    window.location.href = "vaccine1.html";
                    console.log("没有宝宝");
                }
            }else{console.log("获取宝宝失败!")}
        }
    });
}
/*接种记录*/
function ones(childId){
    $(".recordUl").html("");
    ym.ajax({
        url: "/stationVaccine/queryVaccineOrderPlanAll",
        param:{
            childId:childId,
        },
        success: function (data) {
            console.log(data)
            console.log("获取预约记录")
            var html="";
            if(data.obj!=null&&data.obj.length>0){
                for(var i=0;i<data.obj.length;i++){
                    var l=data.obj[i];
                    if(l.rowNumber==undefined||l.rowNumber==null||l.rowNumber==""){
                        l.rowNumber='暂无'
                    }
                    html+='<li>'
                        +'<span class="timeSpan">'
                        +'<span class="orderTime">'+l.orderTime+'</span>'
                        +'<span class="orderNum">预约号数：'+l.rowNumber+'</span>'
                        +'</span>'
                        +'<span class="vaccineSpan">'
                        +'<span class="vaccineName">'+l.vaccineName+'</span>'
                        +'<span class="vaccineAgent">第'+l.agent+'剂</span>'
                        +'</span>'
                        +'<span class="stationName">预约站点：'+l.stationName+'</span>'
                        +'<span class="stationAddr">站点地址：'+l.stationAddress+'</span>'
                        +'<span class="sureDiv">'
                        +'<button class="sureComplete" onclick="sureComplete('+childId+','+l.orderVaccinationId+')">确认接种</button>'
                        +'</span>'
                        +'</li>'
                }
            }else{
                html+='<li class="noMess">'
                    +'<img src="../img/no.jpg" class="noImgMess" />'
                    +'</li>'
            }
            $(".recordUl").append(html);
        }
    });
}
//确认接种
function sureComplete(childId,orderId){
    $("#myConfirm .am-modal-bd").html("是否确认已完成本次预约接种？");
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        //点击确认按钮调用事件
        ym.ajax({
            url:'/stationVaccine/updateUserOrderState',
            type :"post",
            param:{
                orderVaccinationId:orderId,
            },
            success:function(data){
                if(data.res==1){
                    $("#my-modal-loading .am-modal-bd").html("操作成功！");
                }else{
                    $("#my-modal-loading .am-modal-bd").html(data.resMsg);
                }
                $("#my-modal-loading").modal('open');
                setTimeout(function(){
                    $("#my-modal-loading").modal('close');
                    ones(childId);
                },2000)
            }
        });

    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
